/**
 * $Id$ Copyright 2009-2012 Oak Pacific Interactive. All rights reserved.
 */
package com.shuqi.activity.viewport;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.shuqi.application.ShuqiApplication;
import com.shuqi.common.utils.Util;
import com.shuqi.controller.R;

public class CircleProgressBarView extends View {

    public static final int DEFAULT_MAX_VALUE = 100; // 默认进度条最大值

    private static final int DEFAULT_PAINT_WIDTH = Util.dip2px(ShuqiApplication.getContext(), 2); // 默认画笔宽度

    // 画圆所在的距形区域
    RectF mOrignalRectf;

    Paint mPaint;

    private int mMaxProgress = DEFAULT_MAX_VALUE;

    private int mSubCurProgress = 0;

    private int mPaintStrokeWidth = DEFAULT_PAINT_WIDTH;

    private int mPaintColor = R.color.book_paint_blue;

    private int mPaintBgColor = R.color.book_paint_bg;

    public CircleProgressBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mOrignalRectf = new RectF();
        mPaint = new Paint();
    }

    public void setPaintSize(int dp) {
        mPaintStrokeWidth = Util.dip2px(ShuqiApplication.getContext(), dp);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = this.getWidth();
        int height = this.getHeight();
        if (width != height) {
            int min = Math.min(width, height);
            width = min;
            height = min;
        }
        mPaint.setAntiAlias(true); // 设置画笔为抗锯齿
        mPaint.setColor(ShuqiApplication.getContext().getResources().getColor(mPaintBgColor)); // 设置画笔颜色
        canvas.drawColor(Color.TRANSPARENT); // 白色背景
        mPaint.setStrokeWidth(mPaintStrokeWidth); // 线宽
        mPaint.setStyle(Style.STROKE);
        mOrignalRectf.left = mPaintStrokeWidth / 2 + 2; // 左上角x
        mOrignalRectf.top = mPaintStrokeWidth / 2 + 2; // 左上角y
        mOrignalRectf.right = width - mPaintStrokeWidth / 2 - 2; // 左下角x
        mOrignalRectf.bottom = height - mPaintStrokeWidth / 2 - 2; // 右下角y
        canvas.drawArc(mOrignalRectf, -90, 360, false, mPaint);
        mPaint.setColor(ShuqiApplication.getContext().getResources().getColor(mPaintColor));
        canvas.drawArc(mOrignalRectf, -90, ((float) mSubCurProgress / mMaxProgress) * 360, false,
                mPaint);

    }

    public int getMaxProgress() {
        return mMaxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.mMaxProgress = maxProgress;
    }

    public void setProgress(int progress) {
        this.mSubCurProgress = progress;
        this.invalidate();
    }

    public void setProgressBySize(int progress) {
        if (progress != mSubCurProgress) {
            setProgress(progress);
        }
    }

    public void setPaintColor(int c) {
        this.mPaintColor = c;
    }

    public void setPaintBgColor(int c) {
        this.mPaintBgColor = c;
    }

}
